package thn.glf.org.personalfinancial.objData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import thn.glf.org.lib.SQLiteDB;

public class FinancialDay {
	public static String TableName = "tblFinancialDay";
	public static String Field_DayID = "DayID";
	public static String Field_Times = "Times";
	public static String Field_Days = "Days";
	public static String Field_ProductName = "ProductName";
	public static String Field_PriceMoney = "PriceMoney";
	public static String Field_Place = "Place";
	public static String Field_Coordinates = "Coordinates";
	public static String Field_Note = "Note";
	private int DayID;
	private String Times;
	private String Days;
	private String ProductName;
	private double PriceMoney;
	private String Place;
	private String Coordinates;
	private String Note;
	private Context mContext;

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	public FinancialDay() {

	}

	public int getDayID() {
		return DayID;
	}

	public void setDayID(int dayID) {
		DayID = dayID;
	}

	public String getTimes() {
		return Times;
	}

	public void setTimes(String times) {
		Times = times;
	}

	public String getDays() {
		return Days;
	}

	public void setDays(String days) {
		Days = days;
	}

	public String getProductName() {
		return ProductName;
	}

	public void setProductName(String productName) {
		ProductName = productName;
	}

	public double getPriceMoney() {
		return PriceMoney;
	}

	public void setPriceMoney(double priceMoney) {
		PriceMoney = priceMoney;
	}

	public String getPlace() {
		return Place;
	}

	public void setPlace(String place) {
		Place = place;
	}

	public String getCoordinates() {
		return Coordinates;
	}

	public void setCoordinates(String coordinates) {
		Coordinates = coordinates;
	}

	public String getNote() {
		return Note;
	}

	public void setNote(String note) {
		Note = note;
	}

	public ArrayList<FinancialDay> getList() {
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ArrayList<FinancialDay> datalist = new ArrayList<FinancialDay>();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_Coordinates, Field_DayID, Field_Days,
						Field_Note, Field_Place, Field_PriceMoney,
						Field_ProductName, Field_Times}, null, null, null,
				null, Field_Days + "|" + Field_Times, null);
		if (mCursor!=null &&  mCursor.moveToFirst()) {
			do{
				FinancialDay obj=new FinancialDay();
				obj.setDayID(mCursor.getInt(mCursor.getColumnIndex(Field_DayID)));
				obj.setDays(mCursor.getString(mCursor.getColumnIndex(Field_Days)));
				obj.setCoordinates(mCursor.getString(mCursor.getColumnIndex(Field_Coordinates)));
				obj.setNote(mCursor.getString(mCursor.getColumnIndex(Field_Note)));
				obj.setPlace(mCursor.getString(mCursor.getColumnIndex(Field_Place)));
				obj.setPriceMoney(mCursor.getDouble(mCursor.getColumnIndex(Field_PriceMoney)));
				obj.setProductName(mCursor.getString(mCursor.getColumnIndex(Field_ProductName)));
				obj.setTimes(mCursor.getString(mCursor.getColumnIndex(Field_Times)));
				datalist.add(obj);
			}while(mCursor.moveToNext());
		}
		db.close();
		return datalist;

	}
	public ArrayList<FinancialDay> getListInDay(String day) {
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String currentdatetime = formatter.format(currentDate.getTime());
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ArrayList<FinancialDay> datalist = new ArrayList<FinancialDay>();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_Coordinates, Field_DayID, Field_Days,
						Field_Note, Field_Place, Field_PriceMoney,
						Field_ProductName, Field_Times}, Field_Days+"=?", new String []{day}, null,
				null, Field_DayID, null);
		if (mCursor!=null &&  mCursor.moveToFirst()) {
			do{
				FinancialDay obj=new FinancialDay();
				obj.setDayID(mCursor.getInt(mCursor.getColumnIndex(Field_DayID)));
				obj.setDays(mCursor.getString(mCursor.getColumnIndex(Field_Days)));
				obj.setCoordinates(mCursor.getString(mCursor.getColumnIndex(Field_Coordinates)));
				obj.setNote(mCursor.getString(mCursor.getColumnIndex(Field_Note)));
				obj.setPlace(mCursor.getString(mCursor.getColumnIndex(Field_Place)));
				obj.setPriceMoney(mCursor.getDouble(mCursor.getColumnIndex(Field_PriceMoney)));
				obj.setProductName(mCursor.getString(mCursor.getColumnIndex(Field_ProductName)));
				obj.setTimes(mCursor.getString(mCursor.getColumnIndex(Field_Times)));
				datalist.add(obj);
			}while(mCursor.moveToNext());
		}
		db.close();
		return datalist;

	}
	public long Add(FinancialDay obj){
		SQLiteDB db=new SQLiteDB(mContext);
		db.open();
		ContentValues values = new ContentValues();
		values.put(Field_Coordinates, obj.getCoordinates());
		values.put(Field_DayID, obj.getDayID());
		values.put(Field_Days, obj.getDays());
		values.put(Field_Note, obj.getNote());
		values.put(Field_Place, obj.getPlace());
		values.put(Field_PriceMoney, obj.getPriceMoney());
		values.put(Field_ProductName, obj.getProductName());
		values.put(Field_Times, obj.getTimes());
		long flag=db.Insert(TableName, values);
		db.close();
		return flag;
	}
	public int initDayID(String datecheck){
		int id=0;
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String currentdatetime = formatter.format(currentDate.getTime());
		SQLiteDB db=new SQLiteDB(mContext);
		db.open();
		String sql="select MAX(DayID) as STT FROM "+TableName+"  WHERE "+Field_Days+"=?";
		Cursor mCursor=db.rawQuery(sql, new String[]{datecheck});
		if (mCursor!=null &&  mCursor.moveToFirst()) {
			do{
				id=mCursor.getInt(mCursor.getColumnIndex("STT"));
			}while(mCursor.moveToNext());
		}
		db.close();
		return id;
	}
	public double TotalMoneyuse(String day){
		double total=0;
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String currentdatetime = formatter.format(currentDate.getTime());
		SQLiteDB db=new SQLiteDB(mContext);
		db.open();
		String sql="select sum("+Field_PriceMoney+") as total FROM "+TableName+"  WHERE "+Field_Days+"=?";
		Cursor mCursor=db.rawQuery(sql, new String[]{day});
		if (mCursor!=null &&  mCursor.moveToFirst()) {
			do{
				total=mCursor.getDouble(mCursor.getColumnIndex("total"));
			}while(mCursor.moveToNext());
		}
		db.close();
		return total;
	}
	public ArrayList<FinancialDay>getListOfMonth(FinancialMonth financialMonth){
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String currentdatetime = formatter.format(currentDate.getTime());
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ArrayList<FinancialDay> datalist = new ArrayList<FinancialDay>();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_Coordinates,  Field_Days,
						Field_Note, Field_Place, Field_PriceMoney,
						Field_ProductName, Field_Times}, Field_Days+">=? and "+Field_Days+ "<=?", new String []{financialMonth.getMonthFrom(),financialMonth.getMonthTo()}, Field_Days,
						null, null, null);
		if (mCursor!=null &&  mCursor.moveToFirst()) {
			do{
				FinancialDay obj=new FinancialDay();
				obj.setDays(mCursor.getString(mCursor.getColumnIndex(Field_Days)));
				obj.setCoordinates(mCursor.getString(mCursor.getColumnIndex(Field_Coordinates)));
				obj.setNote(mCursor.getString(mCursor.getColumnIndex(Field_Note)));
				obj.setPlace(mCursor.getString(mCursor.getColumnIndex(Field_Place)));
				obj.setPriceMoney(mCursor.getDouble(mCursor.getColumnIndex(Field_PriceMoney)));
				obj.setProductName(mCursor.getString(mCursor.getColumnIndex(Field_ProductName)));
				obj.setTimes(mCursor.getString(mCursor.getColumnIndex(Field_Times)));
				datalist.add(obj);
			}while(mCursor.moveToNext());
		}
		db.close();
		return datalist;
		
	}
}
